﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace X3dTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strFilePath = "D:\\xmltest\\test.xml";
            XmlTextReader objRd = new XmlTextReader(strFilePath);
            listBox1.Items.Clear();

            try
            {
                //List<string> templist = XmlReadAndWrite.getXmlElement(objRd);
                //listBox1.Items.Add(string.Join(",", templist.ToArray()));
                while (objRd.Read())
                {
                    if (objRd.NodeType == XmlNodeType.Element)
                    {
                        if (objRd.HasAttributes)
                        {
                            listBox1.Items.Add("the element " + objRd.Name.ToString()
                                + "has " + objRd.AttributeCount.ToString() + " attributes.");
                            listBox1.Items.Add("they are:");
                            while (objRd.MoveToNextAttribute())
                            {
                                listBox1.Items.Add(objRd.Name + "=" + objRd.Value);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strFilePath = "D:\\test.xml";
            string xpath = "//Transform[@rotation]";
            XmlDocument doc = XmlDocOperate.getXmlDoc(strFilePath);
            XmlNodeList objList = doc.SelectNodes(xpath);//XmlReadAndWrite.getXmlXmlNodeList(strFilePath,xpath);
            foreach (XmlNode objnode in objList)
            {
                XmlNodeReader objRd = new XmlNodeReader(objnode);
                while (objRd.Read())
                {
                    listBox1.Items.Add(objRd.Name);
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filename = "WriteXml.xml";
            XmlTextWriter objTW = new XmlTextWriter(filename, Encoding.UTF8);

            objTW.WriteStartDocument();
            objTW.WriteStartElement("el", "Contact", "http://");
            objTW.WriteEndElement();
            objTW.WriteEndDocument();
            objTW.Flush();
            objTW.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //问题 virtual 
            //X3dInterfaceObject x3d = ActionFactor.LineAction.point2Point("fast"); new Fire();
            //MessageBox.Show(x3d.getAttrs());
            //MessageBox.Show (x3d.toString ());
            ActionFactor.Action action = new ActionFactor.LineAction();
            //lineAction.setAttrs("starP|1 1 1,endP|4 4 4,speed|normal");
            Group g = new Group();
            Transform tr = new Transform();
            tr.DEF = "test";
            Shape sh = new Shape();
            sh.addChildren(new Box() {size ="0.4 0.4 0.5"});
            tr.addChildren(sh);
            g.addChildren(tr);
            action = new ActionFactor.FunctionAction();
            g.addChildren(action .bind2Objec ("test"));

            Transform tr2 = new Transform();
            tr2.DEF = "test2";
            Shape sh2 = new Shape();
            sh2.addChildren(new Sphere() { radius = "0.3" });
            tr2.addChildren(sh2);
            g.addChildren(tr2);
            //action = new ActionFactor.CircleAction ();
            //action.setAttrs("radius|4,speed|5");
            action = new ActionFactor.RotateAction();
            //action.setAttrs("starP|1 1 1,endP|4 4 4,speed|normal");
            g.addChildren (action .bind2Objec ("test2"));
            //g.addChildren(new ActionFactor.LineAction.moveByFunction ("x+=0.6|y+=-0.4|z+=0.3", "fast"));

            
            string mytest = g.toX3dFile();
            StreamWriter sww = new StreamWriter("test.x3d");
            sww.Write(mytest);
            sww.Close();

        }

        private void creatX3D_Click(object sender, EventArgs e)
        {
            string x3dpath = "x3dtest.x3d";
            Box mybox = new Box();
            Material mymaterial = new Material("0.5 0.5 0.5");
            Appearance myapp = new Appearance(mymaterial);
            Shape myshape = new Shape(myapp, mybox);
            //MessageBox.Show(X3DObject.toString(myshape));
            //string myx3d = X3DString.getX3dHead()+"\n"
            //    + X3DObject.toString(myshape)
            //    +"\n"+X3DString.getX3dEnd();
            string myx3d = X3DString.getX3dAll(myshape);
            MessageBox.Show(myx3d);
            //XmlDocument doc = XmlDocOperate.getXmlDoc(x3dpath);            
            XmlDocument doc = XmlDocOperate.loadXmlByStr(myx3d);
            MessageBox.Show("成功保存x3d文件到" + x3dpath);
            doc.Save(x3dpath);
        }

        private void btnTree1_Click(object sender, EventArgs e)
        {
            //string myx3d = X3DString.getX3dAll(Tree.getTreeSence(Tree.getTreeGroup(),2));
            //string myx3d = X3DString.getX3dAll(Tree.getTreeGroup());
            Tree mytree = new Tree(1, "0 1 0", "1.000", 4);

            //修改树叶颜色
            //MessageBox.Show(mytree.getAttrs());
            //mytree.setAttrs("color|0 1 1,size|3");
            //MessageBox.Show(mytree.getAttrs());

            //Scene myscene = new Scene();
            //myscene.addChildren(mytree);

            string myx3d = X3DString.getX3dAll(mytree);
            //MessageBox.Show(myx3d);
            System.IO.File.WriteAllText("treeByIO.x3d", myx3d);
            //XmlDocument doc = XmlDocOperate.loadXmlByStr(myx3d);
            //doc.Save("tree.x3d");
            //MessageBox.Show("成功保存x3d文件");
            //this.axOctagaX1.BeginInit();
            //string x3dUrl = getFileUrl("treeByIO.x3d");
            //this.axOctagaX1.url = x3dUrl;//@"D:\Backup\Visual Studio 2008\Projects\X3dTest\X3dTest\bin\Debug\treeByIO.x3d";

        }

        private void btnTree2_Click(object sender, EventArgs e)
        {
            Tree mytree = new Tree(2, "0 1 0", "1.000", 4);
            string myx3d = X3DString.getX3dAll(mytree);
            System.IO.File.WriteAllText("treeByIO2.x3d", myx3d);
            string x3dUrl = getFileUrl("treeByIO2.x3d");
            //this.axOctagaX1.url = x3dUrl;//@"D:\Backup\Visual Studio 2008\Projects\X3dTest\X3dTest\bin\Debug\treeByIO.x3d";

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void createFire_Click(object sender, EventArgs e)
        {
            Fire fire = new Fire();
            string mytest = fire .toX3dFile ();
            MessageBox.Show(fire.getAttrs());
            StreamWriter sww = new StreamWriter("firetest.x3d");
            sww.Write(mytest);
            sww.Close();
        }

        private string getFileUrl(string fileName)
        {
            return System.IO.Directory.GetCurrentDirectory()+"\\"+fileName;
        }


    }
}
